﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ProductManager_AddRecommandedPrdts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Master.ElementSelectedChange = GetTreeView;
            SetDLLType();
            GetPrdtDataByType(extGVRecPrdt, null);
            GetDiscountOrRecommandedGV(extGVRecommandedPrdt);
            //GetConfigedList("Recommand");
            //GetDiscountOrRecommandedGV(extGVProPrdt);
        }
    }
    #region Initial Object
    GoShopping.BLL.PrdctInfo.DiscountPrdctBean dpb = new GoShopping.BLL.PrdctInfo.DiscountPrdctBean();
    GoShopping.BLL.PrdctInfo.RecomendPrdctBean rpb = new GoShopping.BLL.PrdctInfo.RecomendPrdctBean();
    GoShopping.Model.DiscountPrdct dp = new GoShopping.Model.DiscountPrdct();
    GoShopping.Model.RecomendPrdct rp = new GoShopping.Model.RecomendPrdct();
    GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
    GoShopping.Model.Product prdt = new GoShopping.Model.Product();
    #endregion

    #region Common Method
    private void GetTreeView()
    {
        TreeView tv = Master.MasterTreeView;
        SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
    }

    void GetPrdtDataByType(GridView gv, string filterString)
    {
        if (string.IsNullOrEmpty(filterString))
        {
            gv.DataSource = pb.GetAllProduct();
            gv.DataBind();
        }
        else
        {
            gv.DataSource = pb.GetProductList(filterString);
            gv.DataBind();
        }
    }

    void GetDiscountOrRecommandedGV(GridView gv)
    {
        if (gv.ID == "extGVRecommandedPrdt")
        {
            gv.DataSource = rpb.GetAll();
            gv.DataBind();
        }
    }

    void SetDLLType()
    {
        SetControlCommon.GetDDLType(ddlType1);
    }

    public string GetSupplierName(object obj)
    {
        GoShopping.BLL.ProductSuplyBean psb = new GoShopping.BLL.ProductSuplyBean();
        return psb.GetProductSuply(Convert.ToInt32(obj)).Name;
    }
    /// <summary>
    /// 获取配置的节点
    /// </summary>
    /// <param name="flag">值为:"Pro,Rec,Main,Brand,Friend"</param>
    public void GetRecList()
    {
        extGVRecommandedPrdt.DataSource = new GoShopping.BLL.PrdctInfo.RecomendPrdctBean().GetAll();
        extGVRecommandedPrdt.DataBind();
    }
    public string GetPrdtName(object obj)
    {
        GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
        return pb.GetProduct(Convert.ToInt32(obj)).Name;
    }
    #endregion

    #region 推荐商品设置
    protected void ddlType1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlType1.SelectedValue == "-1")
        {
            GetPrdtDataByType(extGVRecPrdt, null);
            return;
        }
        string filterStr = "AND ProductTypeID=" + ddlType1.SelectedValue;
        GetPrdtDataByType(extGVRecPrdt, filterStr);
    }

    protected void extGVRecPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ddlType1.SelectedValue == "-1")
        {
            this.extGVRecPrdt.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(extGVRecPrdt, null);
            return;
        }
        string filterStr = "AND ProductTypeID=" + ddlType1.SelectedValue;
        this.extGVRecPrdt.PageIndex = e.NewPageIndex;
        GetPrdtDataByType(extGVRecPrdt, filterStr);
    }

    protected void extGVRecPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PrdtEdit")
        {
            hidPrdtId1.Value = e.CommandArgument.ToString();
            lblRecPrdt.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
            //hidImgRec.Value = GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new GoShopping.BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
        }
        else if (e.CommandName == "btnDel")
        {

        }
    }

    protected void btnAddRecPrdt_Click(object sender, EventArgs e)
    {
        rp.Prdctid = Convert.ToInt32(hidPrdtId1.Value);
        rp.Reason = txtReason.Text.Trim();
        rpb.AddOne(rp);
        GetRecList();
    }

    /// <summary>
    /// 配置后的列表事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void extGVRecommandedPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        extGVRecommandedPrdt.PageIndex = e.NewPageIndex;
        GetRecList();
    }

    /// <summary>
    /// 配置后的列表事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void extGVRecommandedPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PrdtDel")
        {
            GoShopping.BLL.PrdctInfo.RecomendPrdctBean rpb = new GoShopping.BLL.PrdctInfo.RecomendPrdctBean();
            rpb.DelRecomendPrdct(Convert.ToInt32(e.CommandArgument));
            GetRecList();
        }
    }
    #endregion
}
